/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myyong.test.user;

import com.mkyong.config.MongoUtil;
import com.myyong.model.Comments;
import com.myyong.model.Post;
import java.util.List;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

/**
 *
 * @author dell
 */
public class PostSearch {

    private MongoOperations mo = MongoUtil.getMongoOperations();

    private void postSearch() {
        System.out.println("#######postSearch");
        Query query = new Query(Criteria.where("comments.replyUser").is("cz"));
        List<Post> postList = mo.find(query, Post.class);
        printUser(postList);
    }
    
      private void postSearchCommentLimit() {
        System.out.println("#######postSearchCommentLimit");
        Query query = new Query(Criteria.where("comments.replyUser").is("cz"));
        query.fields().slice("comments", 5);
        List<Post> postList = mo.find(query, Post.class);
        printUser(postList);
    }

    private void printUser(List<Post> postList) {
        for (Post post : postList) {
            System.out.println("此帖子的评议数有多少:" + post.getComments().size());
            for (Comments comment : post.getComments()) {
                System.out.println(comment);
            }
            System.out.println("########################################");
        }
    }

    public static void main(String[] args) {
        PostSearch postSearch = new PostSearch();
        long time = System.currentTimeMillis();
        postSearch.postSearch();
        System.out.println("用了" + (System.currentTimeMillis() - time) + "毫秒");
        
        
        time = System.currentTimeMillis();
        postSearch.postSearchCommentLimit();
        System.out.println("用了" + (System.currentTimeMillis() - time) + "毫秒");


    }
}
